package com.mxprojects.views
{
	import com.mxprojects.admin.AdminThreads;
	import com.mxprojects.constructors.ButtonLarge;
	import com.mxprojects.gui.BreadCrumbs;
	import com.mxprojects.gui.DefaultDialog;
	import com.mxprojects.gui.ForumDropdown;
	import com.mxprojects.gui.PageNav;
	import com.mxprojects.gui.Resizer;
	import com.mxprojects.gui.ThreadFooter;
	import com.mxprojects.gui.ThreadHeader;
	import com.mxprojects.gui.ThreadItem;
	import com.mxprojects.io.BoardCookie;
	import com.mxprojects.io.Config;
	import com.mxprojects.io.CustomEvent;
	import com.mxprojects.io.DeepLinking;
	import com.mxprojects.io.Permissions;
	import com.mxprojects.io.ServerHandler;
	import com.mxprojects.io.UserControl;
	import com.mxprojects.language.Language;
	import com.mxprojects.language.Word;
	
	import flash.events.MouseEvent;
	import flash.geom.Point;
	
	public class Threads
	{
		public static var threadData:Object;
		public static var isForumRestricted:int;
		
		protected static var threadHeader:ThreadHeader = new ThreadHeader();
		protected static var threadFooter:ThreadFooter = new ThreadFooter();
		
		private static var loadThreads:ServerHandler = new ServerHandler();
		private static var newThreadButton:ButtonLarge;
		private static var forumDropdown:ForumDropdown;
		
		public function Threads(){}
		
		public static function load(forumID:int, page:int = 1):void {
			//get the threads from a forum
			var dataToSend:Object = new Object();
			dataToSend.forumID = forumID;
			dataToSend.page = page;
			dataToSend.maxDaysNewThread = Config.MAX_DAYS_NEW_THREAD;
			dataToSend.threadsPerPage = Config.THREADS_PER_PAGE;
			dataToSend.guestGroupID = Config.GUEST_GROUP_ID;
			
			//if the user is logged in let's send the username along so that we can track threads
			if(BoardCookie.getUser() != "undefined" && BoardCookie.getUser() != null){
				dataToSend.username = BoardCookie.getUser();
				dataToSend.password = BoardCookie.getPassword();
			}
			
			loadThreads.transmitData("viewthreads.php", dataToSend);
			loadThreads.listenForData(showThreads);
			
			//clear the board until it's loaded
			MXP3.clearBoard();
		}
		
		private static function showThreads(event:CustomEvent):void {
			//get the category data
			threadData = event.getServerData();			
			
			//see if there was some oddness with the login
			if(threadData.output == "badLogin"){
				DefaultDialog.show(Language.main(Word.ERROR_LOGIN_INVALID));
				return;
			}
			
			//make sure we are loading a valid forum
			if(threadData.output == "forumNotFound"){
				DefaultDialog.show(Language.main(Word.ERROR_FORUM_NOT_FOUND), true, Word.ERROR, Permissions.returnToHome);
				return;
			}
			
			//make sure they are allowed to be here
			if(threadData.output == "permissionError"){
				Permissions.error();
				return;
			}
			
			//set the current permissions
			Permissions.setup(threadData.isAdmin, threadData.isModerator, threadData.isAllowedToPost);
			
			var nextY:int = 0;
			
//			var newThreadButton2:ButtonLarge = new ButtonLarge(Language.main(Word.NEW_THREAD), newThread);
//			newThreadButton2.y = nextY;
//			newThreadButton2.x = MXP3.wStage - newThreadButton2.width - Config.TEXT_PADDING;
//			MXP3.addContent(newThreadButton2);
//			
//			nextY += newThreadButton2.y + newThreadButton2.height + Config.TEXT_PADDING;
			
			//tell the board where we are
			MXP3.currentLocation = Word.THREADS;
			PageNav.createNav(threadData.currentPage, threadData.totalPages, threadData.forumID);
			BreadCrumbs.createCrumbs(threadData.forumID, threadData.forumTitle);
			DeepLinking.setTitle(threadData.forumTitle);
			
			//put the thread header up
			threadHeader.y = nextY;
			nextY += threadHeader.height - Config.DEFAULT_LINE_HEIGHT;
			MXP3.addContent(threadHeader);
			
			//loop through the threads
			for(var i:uint = 0; i < threadData.threadCount; i++){
				var thread:ThreadItem = new ThreadItem(threadData["thread"+i+"topic"],
														threadData["thread"+i+"topicStarter"],
														threadData["thread"+i+"topicStarterID"],
														threadData["thread"+i+"replies"],
														threadData["thread"+i+"views"],
														threadData["thread"+i+"lastPost"],
														threadData["thread"+i+"lastUsername"],
														threadHeader);
				
				thread.y = nextY;
				thread.name = "thread" + i;
				nextY += thread.height - Config.DEFAULT_LINE_HEIGHT;
				MXP3.addContent(thread);

				thread.setProperties(threadData["thread"+i+"threadID"], 
									 threadData["thread"+i+"locked"], 
									 threadData["thread"+i+"sticky"],
									 threadData["thread"+i+"newPosts"],
									 threadData["announcementDisplayNumber"]);
			}
			
			//put the footer on
			threadFooter.y = nextY;
			threadFooter.resize();
			nextY += threadFooter.height - Config.DEFAULT_LINE_HEIGHT;
			MXP3.addContent(threadFooter);
			
			//add the dropdown menu
			forumDropdown = new ForumDropdown(threadData.forumID);
			forumDropdown.x = Config.TEXT_PADDING;
			forumDropdown.y = nextY + Config.TEXT_PADDING;
			MXP3.addContent(forumDropdown);
			
			//add a "create new thread" button if we are allowed
			newThreadButton = new ButtonLarge(Language.main(Word.NEW_THREAD), newThread);
			newThreadButton.y = nextY + Config.TEXT_PADDING;
			newThreadButton.x = MXP3.wStage - newThreadButton.width - Config.TEXT_PADDING;
			MXP3.addContent(newThreadButton);
			
			//default allow them to post
			setNewThreadButton(true);
			
			//check the permissions
			if(isForumRestricted == 1 && threadData.isAdmin == 0 && threadData.isModerator == 0 || threadData.isAllowedToPost == 0){
				setNewThreadButton(false);
			}
			
			//focus the stage so the backspace works
			newThreadButton.stage.focus = newThreadButton.stage;
			
			//resize the page if we need to
			Resizer.resizeContent();
		}
		
		public static function setNewThreadButton(isEnabled:Boolean):void {
			//this just allows the new thread button to become active or not
			newThreadButton.enabled = isEnabled;
		}
		
		public static function newThread(event:MouseEvent):void {
			//see if they are logged in
			if(UserControl.loggedIn){
				CreatePost.create(CreatePost.ACTION_POST_NEW, threadData.forumID, threadData.forumTitle);
			}else{
				LoginDialog.show();
			}
		}
		
		public static function resize():void {
			threadHeader.resize();
			threadFooter.resize();
			
			//handle the thread items
			for(var i:uint = 0; i < threadData.threadCount; i++){
				var thread:ThreadItem = MXP3.getContent("thread"+i);
				thread.resize();
			}
			
			//move the buttons
			newThreadButton.x = MXP3.wStage - newThreadButton.width - Config.TEXT_PADDING;
		}

	}
}